#include "pcmodel.h"

int PCModel::try_get_out()
{
	int id = -1; //if timeout, return -1.
	std::unique_lock<std::mutex> consumer_lock(consumer_mut_);
	if (0 == size_) {
		std::cv_status status = con_manager_.wait_for(consumer_lock, block_time_);
		if (std::cv_status::timeout == status) {
			return id;
		}
	}
	id = pool_[front_];
	front_ = (front_ + 1) % cMaxPoolSize_;
	size_ -= 1;
	pro_manager_.notify_one();
	return id;
}

void PCModel::put_in(int id)
{
	std::unique_lock<std::mutex> producer_lock(producer_mut_);
	if (size_ >= cMaxPoolSize_) {
		pro_manager_.wait(producer_lock);
	}
	pool_[(front_ + size_)%cMaxPoolSize_] = id;
	size_ += 1;
	con_manager_.notify_one();
}

PCModel::PCModel()
{
	pool_ = new int[cMaxPoolSize_];
	front_ = 0;
	size_ = 0;
}


PCModel::~PCModel()
{
	delete pool_;
}
